package com.ruoyi.sl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlProcess;
import com.ruoyi.sl.service.ISlGprocessService;
import com.ruoyi.sl.service.ISlModuleService;
import com.ruoyi.sl.service.ISlWorkshopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlTeam;
import com.ruoyi.sl.service.ISlTeamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班组Controller
 * 
 * @author admin
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/sl/team")
public class SlTeamController extends BaseController
{
    @Autowired
    private ISlTeamService slTeamService;

    @Autowired
    private ISlModuleService slModuleService;

    @Autowired
    private ISlWorkshopService slWorkshopService;

    @Autowired
    private ISlGprocessService slGprocessService;

    /**
     * 查询班组列表
     */
    @PreAuthorize("@ss.hasPermi('sl:team:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlTeam slTeam)
    {
        startPage();
        List<SlTeam> list = slTeamService.selectSlTeamList(slTeam);
        return getDataTable(list);
    }

    /**
     * 导出班组列表
     */
    @PreAuthorize("@ss.hasPermi('sl:team:export')")
    @Log(title = "班组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlTeam slTeam)
    {
        List<SlTeam> list = slTeamService.selectSlTeamList(slTeam);
        ExcelUtil<SlTeam> util = new ExcelUtil<SlTeam>(SlTeam.class);
        util.exportExcel(response, list, "班组数据");
    }

    /**
     * 获取班组详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:team:query')")
    @GetMapping(value = { "/", "/{teamId}" })
    public AjaxResult getInfo(@PathVariable(value = "teamId", required = false) Long teamId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("modules", slModuleService.selectModuleAll());
        if (StringUtils.isNotNull(teamId))
        {
            SlTeam slTeam = slTeamService.selectSlTeamByTeamId(teamId);
            ajax.put(AjaxResult.DATA_TAG, slTeam);
            //获取模块对应的车间
            if(StringUtils.isNotNull(slTeam.getModuleId())){
                ajax.put("workshops", slWorkshopService.selectSlWorkshopListByModuleId(slTeam.getModuleId()));
            }
            //获取车间对应的大工序
            if(StringUtils.isNotNull(slTeam.getWorkshopId())){
                ajax.put("gprocesss", slGprocessService.selectSlGprocessListByWorkshopId(slTeam.getWorkshopId()));
            }
        }
        return ajax;
        //return success(slTeamService.selectSlTeamByTeamId(teamId));
    }


    /**
     * 新增班组
     */
    @PreAuthorize("@ss.hasPermi('sl:team:add')")
    @Log(title = "班组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlTeam slTeam)
    {
        slTeam.setCreateBy(getUsername());
        return toAjax(slTeamService.insertSlTeam(slTeam));
    }

    /**
     * 修改班组
     */
    @PreAuthorize("@ss.hasPermi('sl:team:edit')")
    @Log(title = "班组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlTeam slTeam)
    {
        slTeam.setUpdateBy(getUsername());
        return toAjax(slTeamService.updateSlTeam(slTeam));
    }

    /**
     * 删除班组
     */
    @PreAuthorize("@ss.hasPermi('sl:team:remove')")
    @Log(title = "班组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable Long[] teamIds)
    {
        return toAjax(slTeamService.deleteSlTeamByTeamIds(teamIds));
    }
}
